package com.pda.yuanjing.pages.codereplace;

import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.pda.yuanjing.R;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.tools.StringUtils;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.VoiceUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeReplaceActivity extends BaseActivity<CodeReplaceModel, CodeReplaceView, CodeReplacePresent> implements CodeReplaceView {
    private String TAG = "CodeResetActivity";

    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.sws)
    Switch sws;
    @BindView(R.id.oet)
    EditText oet;
    @BindView(R.id.net)
    EditText net;
    @BindView(R.id.tvq)
    TextView tvq;
    @BindView(R.id.bt)
    Button bt;

    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_replace);
        ButterKnife.bind(this);
        tvTitle.setText("数码替换");
        sws.setChecked(flag);
        sws.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                flag = isChecked;
            }
        });
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);
        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        if (StringUtils.checkBoxCode(string) || StringUtils.checkCaseCode(string)) {
                            if(flag){
                                net.setText(string);
                            }else {
                                oet.setText(string);
                            }
                            flag = !flag;
                            sws.setChecked(flag);
                        } else {
                            if(flag){
                                tvq.setText("新数码长度不符");
                                voiceUtils.voiceWarn();
                            }else {
                                tvq.setText("原数码长度不符");
                                voiceUtils.voiceWarn();
                            }
                        }
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
    }

    @Override
    public CodeReplaceModel createModel() {
        return new CodeReplaceModelImpl(this);
    }

    @Override
    public CodeReplaceView createView() {
        return this;
    }

    @Override
    public CodeReplacePresent createPresenter() {
        return new CodeReplacePresent();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void tip(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvq.setText(str);
                voiceUtils.voiceWarn();
            }
        });
    }

    @Override
    public void tipSuccess(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                oet.setText("");
                net.setText("");
                flag=false;
                sws.setChecked(flag);
                tvq.setText(str);
            }
        });
    }

    @OnClick({R.id.iv_title_left, R.id.bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.bt:
                if(!StringUtils.checkBoxCode(oet.getText().toString()) && !StringUtils.checkCaseCode(oet.getText().toString())){
                    tvq.setText("原数码长度不符");
                    voiceUtils.voiceWarn();
                    return;
                }
                else if(!StringUtils.checkBoxCode(net.getText().toString()) && !StringUtils.checkCaseCode(net.getText().toString())){
                    tvq.setText("新数码长度不符");
                    voiceUtils.voiceWarn();
                    return;
                }else if(oet.getText().toString().length()!= net.getText().toString().length()){
                    tvq.setText("新旧数码长度不同");
                    voiceUtils.voiceWarn();
                    return;
                }
                else {
                    presenter.codeReplace(oet.getText().toString(),net.getText().toString());
                }
                break;
        }
    }
}
